package com.schoolET.error;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.schoolET.error.costom.*;
import com.schoolET.result.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.util.stream.Collectors;

/*
 *
 *   @Author: Sjy
 *   @Date: 2024/3/6-03-06-19:58
 *   参数全局异常拦截类
 */
@RestController
@ControllerAdvice
public class ParameterError {

    //捕获空指针异常
    /*@ExceptionHandler(NullPointerException.class)
    public String nullPointerException(){
        return "空指针异常";
    }*/

    //参数校验异常拦截
    @ExceptionHandler(value = {BindException.class, ValidationException.class, MethodArgumentNotValidException.class})
    public ResponseEntity<Result<String>> handleValidatedException(Exception e) {
        Result<String> result = null;
        if (e instanceof MethodArgumentNotValidException) {
            // 强制类型转换
            MethodArgumentNotValidException ex = (MethodArgumentNotValidException) e;
            result = Result.error(303, "参数校验异常",
                    ex.getBindingResult().getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage)
                            .collect(Collectors.toList())
            );
        } else if (e instanceof ConstraintViolationException) {
            ConstraintViolationException ex = (ConstraintViolationException) e;
            result = Result.error(303, "参数校验异常",
                    ex.getConstraintViolations().stream()
                            .map(ConstraintViolation::getMessage)
                            .collect(Collectors.toList())
            );
        } else if (e instanceof BindException) {
            BindException ex = (BindException) e;
            result = Result.error(303, "参数校验异常",
                    ex.getAllErrors().stream()
                            .map(ObjectError::getDefaultMessage)
                            .collect(Collectors.toList())
            );
        }
        return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(NotLoginException.class)
    public Result notLoginException() {
        return Result.error().code(501).msg("未能读取到有效token");
    }


    @ExceptionHandler(PasswordException.class)
    public Result passwordException() {
        return Result.error().code(502).msg("账号或密码错误");
    }

    @ExceptionHandler(VerificationCodeException.class)
    public Result verificationCodeException() {
        return Result.error().code(503).msg("无法获取邮箱信息");
    }

    @ExceptionHandler(CaptchaException.class)
    public Result captchaException() {
        return Result.error().code(504).msg("验证码错误");
    }

    @ExceptionHandler(GoodsEditException.class)
    public Result goodsEditException() {
        return Result.error().code(505).msg("商品状态不为未交易状态");
    }

    @ExceptionHandler(PermissionsException.class)
    public Result permissionsException() {
        return Result.error().code(506).msg("权限不足");
    }

    @ExceptionHandler(AdminEditException.class)
    public Result adminEditException() {
        return Result.error().code(507).msg("管理员信息错误");
    }

    @ExceptionHandler(AddAdminException.class)
    public Result addAdminException() {
        return Result.error().code(509).msg("管理员添加错误");
    }

    @ExceptionHandler(MyInfoException.class)
    public Result myInfoException() {
        return Result.error().code(510).msg("只能查看或修改本人");
    }

//    @ExceptionHandler(NoDataException.class)
//    public Result noDataException() {
//        return Result.error().code(400).msg("管理员添加错误");
//    }

    @ExceptionHandler(NoRegionException.class)
    public Result noRegionException() {
        return Result.error().code(508).msg("无此区域");
    }

    @ExceptionHandler(NotPermissionException.class)
    public Result notPermissionException(NotPermissionException notPermissionException) {
        return Result.error().code(506).msg(notPermissionException.getMessage());
    }

    @ExceptionHandler(NotRoleException.class)
    public Result notRoleException(NotRoleException notRoleException) {
        return Result.error().code(506).msg(notRoleException.getMessage());
    }

    @ExceptionHandler(NoPhotoException.class)
    public Result noPhotoException() {
        return Result.error().code(511).msg("无此图片");
    }

    @ExceptionHandler(NoWechatException.class)
    public Result noWechatException() {
        return Result.error().code(512).msg("用户不存在");
    }

    @ExceptionHandler(NoGoodsTypeException.class)
    public Result noGoodsTypeException() {
        return Result.error().code(513).msg("商品类型不存在");
    }

    @ExceptionHandler(NoGoodsException.class)
    public Result noGoodsException() {
        return Result.error().code(514).msg("商品不存在");
    }

    @ExceptionHandler(AddRegionException.class)
    public Result addRegionException() {
        return Result.error().code(515).msg("请重新添加区域");
    }

    @ExceptionHandler(RepeatedJoinRegionException.class)
    public Result repeatedJoinRegionException() {
        return Result.error().code(516).msg("已加入该区域,请勿重复操作");
    }

    @ExceptionHandler(NoNewsException.class)
    public Result noNewsException() {
        return Result.error().code(517).msg("新闻信息不存在");
    }

    @ExceptionHandler(ChangeRegionException.class)
    public Result changeRegionException() {
        return Result.error().code(518).msg("切换区域异常,请勿重复操作");
    }

    @ExceptionHandler(NoWechatRegionException.class)
    public Result noWechatRegionException() {
        return Result.error().code(519).msg("用户区域异常，请勿恶意操作");
    }

    @ExceptionHandler(NumberFormatException.class)
    public Result numberFormatException() {
        return Result.error().code(520).msg("参数类型错误");
    }

    @ExceptionHandler(NoBuyException.class)
    public Result noBuyException() {
        return Result.error().code(521).msg("无此购买此商品");
    }

    @ExceptionHandler(AdminAndRegionException.class)
    public Result adminAndRegionException() {
        return Result.error().code(522).msg("超级管理员无法管理区域");
    }

    @ExceptionHandler(WechatNoRegionException.class)
    public Result wechatNoRegionException() {
        return Result.error().code(523).msg("该用户没有区域显示");
    }

    @ExceptionHandler(AdminNoRegionException.class)
    public Result adminNoRegionException() {
        return Result.error().code(524).msg("该管理员没有管理区域");
    }
}
